package com.kangaroo.myhome.service.impl;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import com.kangaroo.myhome.mapper.TInspProjectXunchaMapper;
import com.kangaroo.myhome.model.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kangaroo.myhome.mapper.TInspSubwayMapper;
import com.kangaroo.myhome.utils.SecurityUtils;
import com.kangaroo.myhome.vo.Option;
import com.kangaroo.myhome.vo.ResponseModel;

import tk.mybatis.mapper.util.StringUtil;

@Service
public class ProjectXunChaService {

    @Autowired
    private TInspProjectXunchaMapper tInspProjectXunchaMapper;

    /**
     * @param name
     * @param page
     * @param rows
     * @return
     */
    public ResponseModel findAll(String projectId, int page, int rows) {
        ResponseModel model = new ResponseModel();

        CommonSearch search = new CommonSearch();
        search.setString1(projectId);
        search.setPagination(page, rows);
        List<TInspProjectXuncha> users = tInspProjectXunchaMapper.selectByPagination(search);
        long count = tInspProjectXunchaMapper.selectByPaginationCount(search);
        model.setCode(200);
        model.setMessage("数据获取成功");
        model.setDataDataGrid(users, count);
        return model;
    }

    /**
     * @param tInspProjectXuncha
     * @return
     */
    public ResponseModel edit(TInspProjectXuncha tInspProjectXuncha) {

        ResponseModel model = new ResponseModel();

        if (tInspProjectXuncha == null) {
            model.setCode(201);
            model.setMessage("参数异常");
            return model;
        }

        int result = 0;
        String id = tInspProjectXuncha.getId();
        if (StringUtil.isEmpty(id)) {
            tInspProjectXuncha.setCreateTime(new Date());
            tInspProjectXuncha.setId(UUID.randomUUID().toString());
            tInspProjectXuncha.setCreator(SecurityUtils.getUsername());
            result = tInspProjectXunchaMapper.insertSelective(tInspProjectXuncha);
        } else {
            result = tInspProjectXunchaMapper.updateByPrimaryKeySelective(tInspProjectXuncha);
        }

        model.setCode(200);
        model.setMessage("操作成功");
        return model;
    }


    public void delete(TInspProjectXuncha tInspProjectXuncha) {
        String ids = tInspProjectXuncha.getId();
        String[] idArr = ids.split(",");
        for (String id : idArr) {
            tInspProjectXunchaMapper.deleteByPrimaryKey(id);
        }
    }

    public int saveFileToBD(TInspProjectXuncha tInspProjectXuncha){
        int insertSuccessNum = tInspProjectXunchaMapper.insert(tInspProjectXuncha);
        return insertSuccessNum;
    }

}
